package com.lsh.payment.core.util.pay.allinpay;

import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.core.exception.BusinessException;
import com.lsh.payment.core.strategy.config.AllinPayConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author peter
 */
@Slf4j
public class SybPayService {

    public static Map<String, String> handleResult(String result) {

        Map<String, String> map = SybUtil.json2Obj(result, Map.class);
        if (map == null) {
            throw new BusinessException(ExceptionStatus.E2001011.getCode(), "下单失败");
        }

        if (AllinPayConfig.REQUEST_SUCCESS.equals(map.get("retcode"))) {
            TreeMap tmap = new TreeMap();
            tmap.putAll(map);
            String sign = tmap.remove("sign").toString();
            String buildSign = SybUtil.sign(tmap, AllinPayConfig.KEY);
            if (buildSign.toLowerCase().equals(sign.toLowerCase())) {
                return map;
            } else {
                throw new BusinessException(ExceptionStatus.E2001011.getCode(), "验证签名失败");
            }
        } else {
            log.info("[handleResult result is] {}",result);
            throw new BusinessException(ExceptionStatus.E2001011.getCode(), map.get("retmsg").toString());
        }
    }

}
